package com.zl.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zl.entity.Agency;
import com.zl.entity.Area;
import com.zl.entity.City;
import com.zl.entity.Collect;
import com.zl.entity.NewsEntity;
import com.zl.entity.Province;
import com.zl.service.AgencyService;
import com.zl.service.CollectService;
import com.zl.service.SelectorService;
import com.zl.util.JsonMapper;
import com.zl.util.SearchUtils;
import com.zl.weixin.entity.UserClient;

/**   
* @Title: AgencyController.java 
* @Package com.zl.controller 
* @Description:机构控制类
* @author chxl   
* @date 2015年8月12日 上午10:58:36 
* @version V1.0   
*/
@Controller
@RequestMapping("agency")
@Scope("prototype")
public class AgencyController {
	@Autowired
	private AgencyService agencyService;
	@Autowired
	private SelectorService selectorService;
	@Autowired
	private CollectService collectService;
	
	@RequestMapping("index")
	public String index(Model model){
		List<Province> provinceList = selectorService.getAllProvince();
		model.addAttribute("provinceList", provinceList);
		
		String modelStr = "citylist";
		List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
		for(Province province:provinceList){
			List<Area> areaList = new ArrayList<Area>();
			areaList.add(province);
			List<City> cityList = selectorService.getCity(province.getId());
			areaList.addAll(cityList);
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("city", areaList);
			mapList.add(map);
		}
		model.addAttribute(modelStr,mapList);
		return "search_jg";
	}
	
	@RequestMapping("list")
	@ResponseBody
	public String list(HttpServletRequest request,Agency agency){
		String areaType = request.getParameter("areaType");
		String area = request.getParameter("area");
		String keyword = request.getParameter("keyword");
		if(StringUtils.isNotBlank(area)){
			if("1".equals(areaType)){
				agency.setProvince(area);
			}else{
				agency.setCity(area);
			}
		}
		agency.setName(keyword);
		int pageSize = Integer.parseInt(request.getParameter("page"));
		int rows = 10;
		
		UserClient user = (UserClient)request.getSession().getAttribute("user");
//		UserClient user = new UserClient();
//		user.setOpenid("oFOgks6AbGnBns_u2teOtiMG711c");
//		
		List<Agency> list = agencyService.selectAgency(agency, pageSize, rows);
		for(Agency agencyC:list){
			List<Collect> collectList = collectService.isExist(user.getOpenid(), agencyC.getId());
			if(collectList!=null&&collectList.size()>0){
				agencyC.setCollections("1");
			}else{
				agencyC.setCollections("0");
			}
		}
		
		String str = JsonMapper.nonDefaultMapper().toJson(list);
		return "{\"success\":\"true\",\"msg\":"+str+"}";
	}
	
	@RequestMapping("detail")
	public String detail(Model model,String id,HttpServletRequest request){
		Agency agency = agencyService.selectAgencyById(id);
		List<NewsEntity> list = agencyService.getNewsByAgencyId(id);
		model.addAttribute("agency", agency);
		UserClient user = (UserClient)request.getSession().getAttribute("user");
//		UserClient user = new UserClient();
//		user.setOpenid("oFOgks6AbGnBns_u2teOtiMG711c");
		for(NewsEntity newsEntity:list){
			List<Collect> collectList = collectService.isExist(user.getOpenid(), newsEntity.getId());
			if(collectList!=null&&collectList.size()>0){
				newsEntity.setCollections("1");
			}else{
				newsEntity.setCollections("0");
			}
		}
		model.addAttribute("list", list);
		return "con_jg";
	}
}
